﻿<%@page contentType="text/html; charset=utf-8" language="java" import="cn.ac.cintcm.synonymous.*"  errorPage="" %>
<%@page import="java.util.List, java.util.Date" %>

<% 
String start = request.getParameter("p");
String w = Util.getUrlParameter(Util.unescape(request.getQueryString()),"w");
String type = request.getParameter("t"); 
String f = Util.getUrlParameter(Util.unescape(request.getQueryString()),"f");  
String edited = request.getParameter("e"); 
String term = Util.getUrlParameter(Util.unescape(request.getQueryString()),"n"); 
String method = request.getParameter("m"); 
String selectedId = request.getParameter("s");
String author = request.getRemoteAddr();

DataSource ds = CSVDataSource.createDataSource(Config.getDataSourceFile());
Dictionary dict = CSVDictionary.getInstance(Config.getDictFile());
Job job = Job.getJobInstance(ds, dict);
WordEditCtl editCtl = WordEditCtl.getInstance(ds, job, dict);

if (method != null && method.equals("rp")) { 
   	editCtl.process(); 
}

ResultTerms resultTerms = editCtl.getResultTerms() ;
Message msg  = null;

if (application.getAttribute("count") == null) {
	application.setAttribute("count", new Long(Word.getCounter()));
} 
Long counter = (Long) application.getAttribute("count");
if (w != null && !w.trim().equals("")) {
  if ( type != null) {
	  if (type.charAt(0) == EditCtl.ADD_FORMAL) {
	    Word word = new Word();
	    word.setName(w);
	    word.setAlias(false);
	    word.setCreated(new Date());
	    word.setAuthor(author);
	    counter = new Long(counter.longValue() + 1L);
	    word.setId(counter.longValue());
	    msg = editCtl.addWord(word, null);
	  } else if (type.charAt(0) == EditCtl.ADD_INFORMAL) {
	   	  msg = new Message();
        if (f != null) {
            Word word = dict.get(f);
            if (word == null) {
                word = new Word();
                word.setName(f);
                word.setAlias(false);
                word.setCreated(new Date());
                word.setAuthor(author);
                word.setFormalName(f);
	              counter = new Long(counter.longValue() + 1L);
                word.setId(counter.longValue());
                msg = editCtl.addWord(word, null);
            } else {
                msg.setSuccess(true);
            }
            Word ifmw = new Word();
            ifmw.setName(w);
            ifmw.setAlias(true);
            ifmw.setCreated(new Date());
            ifmw.setAuthor(author==null?"":author);
            ifmw.setFormalName(f);
	          counter = new Long(counter.longValue() + 1L);
            ifmw.setId(counter.longValue());
            Message m2 = editCtl.addWord(word, ifmw);;
            msg.setSuccess(msg.isSuccess() && m2.isSuccess());
            msg.setMsg(msg.getMsg() + m2.getMsg());
        }
		} else {
		   msg = editCtl.change(w, type, f, author);
	  }
    if (msg.isSuccess()) {
        resultTerms.removeTerm(w);
    }
	}
}
application.setAttribute ("count", counter); 

ResultFilterTerms ft;
if(term !=null && !term.trim().equals("")) {
   ft = new ResultFilterTerms (resultTerms);
   ft.filterTerm(term);
   resultTerms = ft;
}

if (edited != null && !edited.trim().equals("")) {
  ft = new ResultFilterTerms (resultTerms);
  ft.filter(edited);
  resultTerms = ft;
}
DataSourceCtl dctl = new DataSourceCtl((PagedData)resultTerms); 
String urls = "words.jsp?e=" + (edited==null?"":edited) + "&s=" +selectedId + "&"; 

if (start==null) { 
	start = "1";
}
List list = dctl.getData(start);
String navigation = dctl.getNavigation(urls, start); 


List sourcelist = null;
String urls2 = "words.jsp?" + request.getQueryString() + "&"; 
String moreurl ="moredata.jsp?w=";
DataSourceCtl dctl2  = null;
if (method != null) {
  if (method.equals("d")) {
      if (w != null && !w.equals("")) {
          dctl2 = new DataSourceCtl((PagedData) new SelectedDataSource(editCtl.getSource(w)));

          sourcelist = dctl2.getData("1");
      }
  } else if (method.equals("sd")) {
   	msg = editCtl.save(); 
  } 
}



%>

<html>
<head>
  <meta content="text/html; charset=utf-8"
 http-equiv="content-type">
  <title>正异名修正_词修正</title>
  <style>
#messageBox{
border-right: 1px solid #000000;
position: absolute;
width: 37px;
height: 130px;
z-index: 1;
background-color: #C0C0C0;
border-style: solid;
border-width: 1px;
display:none;
}
#closeButt{
width: 100%;
height: 10px;
z-index: 1;
cursor: pointer;
left: 0px;
top: 0px;
background-color: #808080;
}
#contents{
width: 100%;
height: auto;
z-index: 2;
}
  </style>
  <script language="javascript">
<!-- Copyright 2006,2007 Bontrager Connection, LLC
// http://bontragerconnection.com/ and http://www.willmaster.com/
// Version: July 28, 2007
var cX = 0; var cY = 0; var rX = 0; var rY = 0;
var selectedWord = " ";
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;
}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;
}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }else {
document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d) {
if(self.pageYOffset) {
rX = self.pageXOffset;
rY = self.pageYOffset;
} else if(document.documentElement && document.documentElement.scrollTop) {
rX = document.documentElement.scrollLeft;
rY = document.documentElement.scrollTop;
} else if(document.body) {
rX = document.body.scrollLeft;
rY = document.body.scrollTop;
}
if(document.all) {
cX += rX; cY += rY;
}
d.style.left = (cX+10) + "px";
d.style.top = (cY+10) + "px";
}
function HideContent(d) {
if(d.length < 1) {
return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d, word) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd);
dd.style.display = "block";
selectedWord = word;
document.getElementById("formalName").value = word;
document.getElementById("newName").value = word;
document.getElementById("rnewName").value = word;
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
var dd = document.getElementById(d);
AssignPosition(dd);
if(dd.style.display == "none") {
dd.style.display = "block"; } else { dd.style.display = "none";
}
}
function showItems(word) {
window.onbeforeunload = null;
window.location = "<%=urls%>p=<%=start%>&m=d&w=" + escape(word)+"&n=" + escape('<%=term%>');
}
function addFormal() {
	window.onbeforeunload = null;
	var formal = document.getElementById("formalName"); 
  if (formal.value != selectedWord) {
  	window.location = "<%=urls%>p=<%=start%>&t=<%=EditCtl.ADD_INFORMAL%>&w=" +escape(selectedWord) + "&f=" + escape(formal.value)+"&n=" + escape('<%=term%>');
  } else {
  	window.location = "<%=urls%>p=<%=start%>&t=<%=EditCtl.ADD_FORMAL%>&w=" + escape(selectedWord)+"&n=" + escape('<%=term%>');
 }
} 
function deleteWord() {
window.onbeforeunload = null;
window.location = "<%=urls%>p=<%=start%>&t=<%=EditCtl.DELETE_WORD%>&w=" + escape(selectedWord)+"&n=" + escape('<%=term%>');
}
 function deleteDictWord() {
window.onbeforeunload = null;
window.location = "<%=urls%>p=<%=start%>&t=<%=EditCtl.DELETE_DICT_WORD%>&w=" + escape(selectedWord)+"&n=" + escape('<%=term%>');
} function modifyWord() {
window.onbeforeunload = null;
var newName = document.getElementById("newName"); window.location.href = "<%=urls%>p=<%=start%>&t=<%=EditCtl.MODIFY_WORD%>&w=" + escape(selectedWord) + "&f=" + escape(newName.value)+"&n=" + escape('<%=term%>');
}
function modifyRWord() {
window.onbeforeunload = null;
var newName = document.getElementById("rnewName"); window.location.href = "<%=urls%>p=<%=start%>&t=<%=EditCtl.MODIFY_WORD%>&w=" + escape(selectedWord) + "&f=" + escape(newName.value)+"&n=" + escape('<%=term%>');
}
function navigation(page) {
	window.onbeforeunload  = null; 
	window.location.href = "<%=urls%>p="+page +"&n=" + escape('<%=term%>');
} 

function filterCategory(condition) {
	window.onbeforeunload  = null;  
	window.location.href ="words.jsp?s=<%=selectedId%>&e="+condition +"&n=" + escape('<%=term%>');
}

function filterName(event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		window.onbeforeunload  = null;  
		var fname =  document.getElementById("term");
		window.location.href ="words.jsp?s=<%=selectedId%>&n="+escape(fname.value);
  }
}

function edit(category, word) {
	if (category == '<%=Record.UNRECOGNIZED%>'){
		  ShowContent('unrecognized', word);
	} else {
		  ShowContent('recognized', word);
	}
}
function save() {
	window.onbeforeunload  = null;
	window.location.href = "<%=urls%>p=<%=start%>&m=sd";
}

function process() {
	window.onbeforeunload  = null;
	window.location.href = "<%=urls%>p=<%=start%>&m=rp";
} 

function changeWindow() {
	window.onbeforeunload  = null;
	window.location.href = "index.jsp?s=<%=selectedId%>";
} 
function viewWord() {
	
	var urls = "dict.jsp?word="+escape(selectedWord) ;
	window.open(urls);
} 

function more() {
	var murl= "<%=moreurl%>" + escape('<%=w%>');
	window.open(murl);
}

//--> </script>
</head>
<body>
<div id="unrecognized" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="addFormal();return true;" href="#">添加为正名</a><input id="formalName" value=""><br>
<a onClick="deleteWord(); return true;" href="#">从数据源删除</a><br>
<a onClick="modifyWord(); return true; " href="#">修改数据源词</a><input id="newName" value=""><br>
<div onClick="HideContent('unrecognized'); return true;">关闭</div>
</div> 

<div id="recognized" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="viewWord()" href="#">查词典</a><br>
<a onClick="deleteWord(); return true;" href="#">从数据源删除</a><br>
<a onClick="deleteDictWord(); return true;" href="#">从词典删除</a><br>
<a onClick="modifyRWord(); return true; " href="#">修改数据源词</a><input id="rnewName" value=""><br>
<div onClick="HideContent('recognized'); return true;">关闭</div>
</div>  

<div id="word" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="deleteDictWord(); return true;" href="#">从词典删除</a><br>
<div onClick="HideContent('word'); return true;">关闭</div>
</div>   

<table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td>
      <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td colspan="3" rowspan="1"><%=(msg==null?"":msg.toString())+ editCtl.getEditMessage()%>&nbsp;<a onClick="process();" href="#">重新处理数据</a>&nbsp;<a onClick="changeWindow();" href="#">切换到条处理</a>&nbsp;<a href="dict.jsp" target="_blank">浏览词典</a></td>
          </tr>
          <tr>
            <td colspan="3" rowspan="1"><%=navigation%></td>
          </tr>
          <tr>
            <td>
            <select name="type" onChange="filterCategory(this.options[this.selectedIndex].value);">
            <option value="">全部</option>
            <option value="<%=Record.UNRECOGNIZED%>" <%=(edited!=null&&edited.equals(Record.UNRECOGNIZED +""))?"selected":""%>>未识别</option>
            <option value="<%=Record.FORMAL%>" <%=(edited!=null&&edited.equals(Record.FORMAL +""))?"selected":""%>>正名被识别</option>
            <option value="<%=Record.ALIAS%>" <%=(edited!=null&&edited.equals(Record.ALIAS +""))?"selected":""%>>异名转正名</option>
            </select>
            </td>
            <td>词名<input id="term" value="<%=term!=null?term:""%>" onkeypress="filterName(event);"></td>
            <td>原文数目</td>
          </tr>
  <%
  if (list != null) {
  String[] data = null;
	  for (int i = 0; i < list.size(); i++) {
	      data = (String[]) list.get(i);
	%>
	          <tr>
	            <td><%
	             	String str = "";
	             	String category = data[Term.ATTRIBUTE_TYPE] ;
	             	if (category != null) {
	             	    int t = Integer.parseInt(category);
			            	switch (t) {
					            	case Record.UNRECOGNIZED:
					            		str = "未识别";
					            		break;
					            	case Record.FORMAL:
					            	  str = "识别为正名";
					            		break;
					            	case Record.ALIAS:
					            	  str = "识别为异名";
					            		break;
			            	} 
	            	}
	            	out.write(str);
	            	%></td>
	            <td><a onClick="edit('<%=category%>', '<%=data[Term.ATTRIBUTE_NAME]%>');" href="#"><%=data[Term.ATTRIBUTE_NAME]%></a></td>
	            <td><a onClick="showItems('<%=data[Term.ATTRIBUTE_NAME]%>');" <%=(w!=null && w.equals(data[Term.ATTRIBUTE_NAME]))?"":"href=\"#\"" %>><%=data[Term.ATTRIBUTE_COUNT]%></a></td>
	          </tr>
	<%
	   }
   }
  %>
        </tbody>
      </table>
      </td>
 <%if (sourcelist != null) {%>
      <td>
      <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td colspan="3" rowspan="1"><a onClick="more();"><%=dctl2.getTotalPages() > 1?"更多数据":""%></a></td>
          </tr>
          <tr>
            <td>序号</td>
            <td>name</td>
            <td>column</td>
          </tr>
<%
  if (sourcelist != null) {
    String[] sourcedata = null;
	  for (int i = 0; i < sourcelist.size(); i++) {
	      sourcedata = (String[]) sourcelist.get(i);
%>
          <tr>
            <td><%=sourcedata[0]%></td>
            <td><%=sourcedata[2]%></td>
            <td><%=sourcedata[1]%></td>
          </tr>
 <%
     }
  }
 %>
        </tbody>
      </table>
      </td>
 <%}%>      
       
    </tr>
  </tbody>
</table>
<br>
</body>
</html>
